package com.ojoin.trade.t0loan_core.web.controller;

import java.net.URLDecoder;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ojoin.trade.common.exception.BusinessException;
import com.ojoin.trade.common.utils.CommonUtils;
import com.ojoin.trade.common.web.controller.SimpleBaseController;
import com.ojoin.trade.common.web.domain.Result;
import com.ojoin.trade.common.web.domain.Result.Status;
import com.ojoin.trade.common_core.utils.TradeUtils;
import com.ojoin.trade.common_core.web.service.impl.DataVersionValidationService;
import com.ojoin.trade.t0loan_core.web.domain.TbIntentUser;
import com.ojoin.trade.t0loan_core.web.domain.TbT0LoanFinalApplication;
import com.ojoin.trade.t0loan_core.web.domain.TbT0LoanLiveApplication;
import com.ojoin.trade.t0loan_core.web.domain.TbT0LoanLiveInfoVO;
import com.ojoin.trade.t0loan_core.web.domain.TbT0LoanTodoTaskVO;
import com.ojoin.trade.t0loan_core.web.service.IntentUserBaseService;
import com.ojoin.trade.t0loan_core.web.service.T0LoanFinalBaseService;
import com.ojoin.trade.t0loan_core.web.service.T0LoanLiveBaseService;
import com.ojoin.trade.t0loan_core.web.service.T0LoanTransanctionLogBaseService;

@Controller
@RequestMapping("/operate")
public class PayUserController  extends SimpleBaseController {

	private static Logger log = LoggerFactory
			.getLogger(PayUserController.class);

	@Autowired
	private DataVersionValidationService validateService;
	
	public DataVersionValidationService getValidateService() {
		return validateService;
	}
	
//	@Autowired
//	private AdminUserBaseService adminuserservice;
//
//	public AdminUserBaseService getAdminuserservice() {
//		return adminuserservice;
//	}

	@Autowired
	private T0LoanLiveBaseService liveservice;

	public T0LoanLiveBaseService getLiveservice() {
		return liveservice;
	}

	@Autowired
	private T0LoanFinalBaseService finalservice;

	public T0LoanFinalBaseService getFinalservice() {
		return finalservice;
	}

	@Autowired
	private T0LoanTransanctionLogBaseService transservice;

	public T0LoanTransanctionLogBaseService getTransservice() {
		return transservice;
	}

	@Autowired
	private IntentUserBaseService intentUserService;

	public IntentUserBaseService getIntentUserService() {
		return intentUserService;
	}
	
	@ResponseBody
	@RequestMapping(value = "/queryliveinfo", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public Result queryLiveInfo(@RequestParam String joinuserid) {
		 if(StringUtils.isEmpty(joinuserid)){
		 return new Result(Status.ERROR, "九盈用户id为空，请检查.");
		 }
		 try {
		 TbT0LoanLiveApplication livevo =
		 getLiveservice().queryNewlyT0LoanLiveInfoByJoinUserID(CommonUtils.getInt(joinuserid));
		 // String result=JSONObject.toJSONString(livevo);
		 return new Result(Status.OK, livevo);
		 } catch (Exception e) {
		 log.error("根据九盈用户id查询用户的最近一次T+0贷款申请记录报错", e);
		 if (e instanceof BusinessException)
				return new Result(Status.ERROR, e.getMessage());
			
		 return new Result(Status.ERROR, "根据九盈用户id查询用户的最近一次T+0贷款申请记录报错，请重试");
		 }
//		TbT0LoanLiveApplication testvo = new TbT0LoanLiveApplication();
//		testvo.setId(1);
//		testvo.setLoanstatus(4);
//		testvo.setRatio("3%");
//		testvo.setIdOfTbMerchantUser(3333);
//		return new Result(Status.OK, testvo);
	}

	@ResponseBody
	@RequestMapping(value = "/queryfinalinfo", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public Result queryFinalInfo(@RequestParam String joinuserid) {
		 if(StringUtils.isEmpty(joinuserid)){
		 return new Result(Status.ERROR, "九盈用户id为空，请检查.");
		 }
		 try {
		 TbT0LoanFinalApplication finalvo =
		 getFinalservice().queryTxLoanFinalInfoByJoinUserID(CommonUtils.getInt(joinuserid));
		 //String result=JSONObject.toJSONString(finalvo);
		 return new Result(Status.OK, finalvo);
		 } catch (Exception e) {
		 log.error("根据九盈用户id查询用户的最近一次T+0贷款申请记录报错", e);
		 if (e instanceof BusinessException)
				return new Result(Status.ERROR, e.getMessage());
			
		 return new Result(Status.ERROR, "根据九盈用户id查询用户的最近一次T+0贷款申请记录报错，请重试");
		 }
//		TbT0LoanFinalApplication testvo = new TbT0LoanFinalApplication();
//		testvo.setExpectedLoanTime("11:00");
//		testvo.setRatio("3%");
//		testvo.setRmDailyThreshold("1000000");
//		return new Result(Status.OK, testvo);
	}

//	@ResponseBody
//	@RequestMapping(value = "/queryLiveInfoByStatus", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
//	public Result queryLiveInfoByStatus(@RequestParam String loanstatus, Pageable page) {
//		if (StringUtils.isEmpty(loanstatus)) {
//			return new Result(Status.ERROR, "审核状态为空，请检查.");
//		}
//		try {
//			List<TbT0LoanLiveInfoVO> livelist = getLiveservice()
//					.queryAllT0LoanLiveInfoByStatus(CommonUtils.getInt(loanstatus), page);
//			return new Result(Status.OK, livelist);
//		} catch (Exception e) {
//			log.error("查询状态为" + loanstatus + "的T+0贷款申请记录报错", e);
//			if (e instanceof BusinessException)
//				return new Result(Status.ERROR, e.getMessage());
//			
//			return new Result(Status.ERROR, "查询状态为" + loanstatus
//					+ "的T+0贷款申请记录报错，请重试");
//		}
//	}

//	@ResponseBody
//	@RequestMapping(value = "/updateLiveInfoStatus", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
//	public Result updateLiveInfoStatus(HttpServletRequest request,
//			@RequestParam String liveid, @RequestParam String loanstatus, @RequestParam String pagets) {
//		if (StringUtils.isEmpty(liveid)) {
//			return new Result(Status.ERROR, "申请记录id为空，请检查.");
//		}
//		
//		if (StringUtils.isEmpty(loanstatus)) {
//			return new Result(Status.ERROR, "审核状态为空，请检查.");
//		}
//		
//		//操作数据版本校验
//		TbT0LoanLiveApplication validateVO = new TbT0LoanLiveApplication();
//		validateVO.setId(CommonUtils.getInt(liveid));
//		validateService.validateVO(validateVO, pagets);
//				
//		try {
//			getLiveservice().updateT0LoanLiveInfoByLiveIDsAndStatus(
//					new int[] { CommonUtils.getInt(liveid) },
//					CommonUtils.getInt(loanstatus));
//			return new Result(Status.OK, "更新成功");
//		} catch (Exception e) {
//			log.error("查询状态为" + loanstatus + "的T+0贷款申请记录报错", e);
//			if (e instanceof BusinessException)
//				return new Result(Status.ERROR, e.getMessage());
//			
//			return new Result(Status.ERROR, "查询状态为" + loanstatus
//					+ "的T+0贷款申请记录报错，请重试");
//		}
//	}
	
	@ResponseBody
	@RequestMapping(value = "/updateT0LoanLiveInfoFeeAndRatioByLiveID", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public Result updateT0LoanLiveInfoFeeAndRatioByLiveID(HttpServletRequest request, int liveid, String fee, String ratio, String expected_loan_time, String pagets) {
		if (liveid == 0) {
			return new Result(Status.ERROR, "贷款申请id为空，请检查.");
		}
		if (StringUtils.isEmpty(fee) && StringUtils.isEmpty(ratio)) {
			return new Result(Status.ERROR, "手续费率和利息费率不可以同时为空，请检查.");
		}
//		if (StringUtils.isEmpty(ratio)) {
//			return new Result(Status.ERROR, "利息费率为空，请检查.");
//		}
		if (StringUtils.isEmpty(expected_loan_time)) {
			return new Result(Status.ERROR, "期望放款时间为空，请检查.");
		}
		//操作数据版本校验
		TbT0LoanLiveApplication validateVO = new TbT0LoanLiveApplication();
		validateVO.setId(liveid);
		validateService.validateVO(validateVO, pagets);
		
		try {
			int creditorid = TradeUtils.getOperatorCompany(request);
			getLiveservice().updateT0LoanLiveInfoFeeAndRatioByLiveID(liveid, fee, ratio, expected_loan_time, creditorid);
            return new Result(Status.OK, liveid);
		} catch (Exception e) {
			log.error("修改T+0贷款申请记录的设定手续费和利率，并更新状态为2报错", e);
			if (e instanceof BusinessException)
				return new Result(Status.ERROR, e.getMessage());
			
			return new Result(Status.ERROR, "修改T+0贷款申请记录的设定手续费和利率，并更新状态为2报错，请重试");
		}
	}
	
	@ResponseBody
	@RequestMapping(value = "/queryIntentUserListByContactflag", method = RequestMethod.GET)
	public Result queryIntentUserListByContactflag(HttpServletRequest request, 
			String contactflag) {
		try {
			List<TbIntentUser> list = getIntentUserService().queryIntentUserListByContactflag(contactflag);
             return new Result(Status.OK, list);
		} catch (Exception e) {
			log.error("查询状态" + contactflag + "的意向合作商户列表报错", e);
			if (e instanceof BusinessException)
				return new Result(Status.ERROR, e.getMessage());
			
			return new Result(Status.ERROR, "查询状态" + contactflag + "的意向合作商户列表报错，请重试");
		}
	}
	
	@ResponseBody
	@RequestMapping(value = "/updateContactFlagByID", method = RequestMethod.POST)
	public Result updateContactFlagByID(HttpServletRequest request, 
			int id, String memo, String contactflag) {
		try {
			int count = getIntentUserService().updateContactFlagByID(new int[] {id}, memo, contactflag);
            return new Result(Status.OK, count);
		} catch (Exception e) {
			log.error("更新id" + id + "意向合作商户的状态值为" +contactflag + "报错", e);
			if (e instanceof BusinessException)
				return new Result(Status.ERROR, e.getMessage());
			
			return new Result(Status.ERROR, "更新id" + id + "意向合作商户的状态值为" +contactflag + "报错，请重试");
		}
	}
	
	@ResponseBody
	@RequestMapping(value = "/updateT0LoanLiveInfoApplyNoPassByLiveID", method = RequestMethod.POST)
	public Result updateT0LoanLiveInfoApplyNoPassByLiveID(HttpServletRequest request, 
			int liveid, String memo, String pagets) {
		if (liveid == 0) {
			return new Result(Status.ERROR, "贷款申请id为空，请检查.");
		}
		//操作数据版本校验
		TbT0LoanLiveApplication validateVO = new TbT0LoanLiveApplication();
		validateVO.setId(liveid);
		validateService.validateVO(validateVO, pagets);
		
        try{
        memo= URLDecoder.decode(memo);
        }catch(Exception e){

        }
		try {
			int count = getLiveservice().updateT0LoanLiveInfoApplyNoPassByLiveID(liveid, memo);
            return new Result(Status.OK, count);
		} catch (Exception e) {
			log.error("审核用户T+0业务申请不通过，提交不通过原因并更新状态为-1报错", e);
			if (e instanceof BusinessException)
				return new Result(Status.ERROR, e.getMessage());
			
			return new Result(Status.ERROR, "审核用户T+0业务申请不通过，提交不通过原因并更新状态为-1报错，请重试");
		}
	}
	
	@ResponseBody
	@RequestMapping(value = "/queryPayUserTodoTaskList", method = RequestMethod.GET)
	public Result queryPayUserTodoTaskList(HttpServletRequest request) {
		try {
			List<TbT0LoanTodoTaskVO> list = getLiveservice().queryAdminUserTodoTaskList();
            return new Result(Status.OK, list);
		} catch (Exception e) {
			log.error("查询后台管理员代办任务列表报错", e);
			if (e instanceof BusinessException)
				return new Result(Status.ERROR, e.getMessage());
			
			return new Result(Status.ERROR, "查询后台管理员代办任务列表报错，请重试");
		}
	}
	
}
